var $ = require('../../utils/Ajax.js');
Page({
  data: {
    // 判断是不是开发者
    Developer: false,
    PleaseLogin: true,
    UserAttribute: "我要成为觅探",
    UserAttributeBol: false,
    ClickAttribute: 0,
    UserInfor: null,
    Signature: "随便说点什么吧！",
    SignatureFocus: false,
    SignatureDisabled: true,
    Status: null,
    Sex: 0,
    MyOrderNum: 0
  },
  onLoad: function (options){
    wx.setNavigationBarTitle({
      title: "个人中心"
    }),
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#FDD436'
    });
  },
  onShow: function () {
    var that = this;
    $.Ajax("api/x_user", "POST", {}, function (res) {
      console.log(res)
      if (res.s) {
        wx.setStorageSync('UserInfor', res.d);

        // 判断是否为开发者，展示更新日志
        if (res.d.user.openId == "oqWo-5YFSYwowWsPJuoAkdL1-9hk") {
          that.setData({
            Developer: true
          })
        }

      }

    }) 

    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      that.setData({
        UserInfor: null,
        Signature: null,
        PleaseLogin: false
      })
      wx.showToast({
        title: "请点击登录",
        icon: 'none',
        duration: 2000
      })
    } else if (UserInfor.leadway == null) {
      that.setData({
        UserInfor: UserInfor,
        Signature: UserInfor.user.autograph,
        Sex: UserInfor.user.gender,
        PleaseLogin: true,
        MyOrderNum: 0
      })
    } else if (UserInfor.leadway.status == 0) {
      that.setData({
        UserInfor: UserInfor,
        Signature: UserInfor.user.autograph,
        Sex: UserInfor.user.gender,
        PleaseLogin: true,
        Status: UserInfor.leadway.status,
        UserAttribute: "正在审核中",
        MyOrderNum: 0
      })
    } else if (UserInfor.leadway.status == 1) {
      that.setData({
        UserInfor: UserInfor,
        Signature: UserInfor.user.autograph,
        Sex: UserInfor.user.gender,
        PleaseLogin: true,
        Status: UserInfor.leadway.status,
        UserAttribute: "我的觅探",
        ClickAttribute: UserInfor.leadway.id,
        UserAttributeBol: true,
        MyOrderNum: UserInfor.leadway.s_order_count
      })
    } else if (UserInfor.leadway.status == 2) {
      wx.setStorageSync('UserManey', 0)
      that.setData({
        UserInfor: UserInfor,
        Signature: UserInfor.user.autograph,
        Sex: UserInfor.user.gender,
        PleaseLogin: true,
        Status: UserInfor.leadway.status,
        UserAttribute: "审核失败",
        MyOrderNum: 0
      })
    }
  },

  // 点击登录按钮
  ClickLogin: function(){
    var that = this;
    wx.login({
      success: function (res) {
        var Code = res.code;
        wx.getUserInfo({
          success: function (res_) {
            $.Ajax("api/x_login","POST", {
              wx_encryptData: res_.encryptedData,
              wx_iv: res_.iv,
              wx_code: Code
            },function(res){
              console.log(res)
              if(res.s){
                wx.setStorageSync('Token', 'Bearer ' + res.d.token);
                wx.setStorageSync('UserInfor', res.d);
                if (res.d.leadway == null) {
                  wx.setStorageSync('UserManey', 0);
                  that.setData({
                    UserInfor: res.d,
                    Signature: res.d.user.autograph,
                    Sex: res.d.user.gender,
                    PleaseLogin: true,
                    MyOrderNum: 0
                  })
                } else if (res.d.leadway.status == 0) {
                  wx.setStorageSync('UserManey', 0);
                  that.setData({
                    UserInfor: res.d,
                    Signature: res.d.user.autograph,
                    Sex: res.d.user.gender,
                    PleaseLogin: true,
                    Status: 0,
                    UserAttribute: "正在审核中",
                    MyOrderNum: 0
                  })
                } else if (res.d.leadway.status == 1) {
                  wx.setStorageSync('UserManey', res.d.leadway.balance)
                  that.setData({
                    UserInfor: res.d,
                    Signature: res.d.user.autograph,
                    Sex: res.d.user.gender,
                    PleaseLogin: true,
                    UserAttribute: "我的觅探",
                    Status: 1,
                    ClickAttribute: res.d.leadway.id,
                    UserAttributeBol: true,
                    MyOrderNum: res.d.leadway.s_order_count
                  })
                } else if (res.d.leadway.status == 2) {
                  wx.setStorageSync('UserManey', 0)
                  that.setData({
                    UserInfor: res.d,
                    Signature: res.d.user.autograph,
                    Sex: res.d.user.gender,
                    PleaseLogin: true,
                    Status: 2,
                    UserAttribute: "审核失败",
                    MyOrderNum: 0
                  })
                }

                // 判断是否为开发者，展示更新日志
                if (res.d.user.openId == "oqWo-5YFSYwowWsPJuoAkdL1-9hk") {
                  that.setData({
                    Developer: true
                  })
                }

              }else{
                wx.showToast({
                  title: res.m,
                  icon: 'none',
                  image: '../../images/err.png',
                  duration: 200000
                })
              }
            });
          }
        })
      }
    });
  },
  // 修改个性签名
  ClickSignature: function(){
    this.setData({
      SignatureFocus: true,
      SignatureDisabled: false
    })
  },

  SignatureInput: function(e){
    this.setData({
      Signature: e.detail.value
    })
  },

  // 个性签名失去焦点
  SignatureBlur: function(){
    var that = this;
    $.Ajax("api/x_autograph", "POST", {
      autograph: that.data.Signature
    }, function (res) {
      if(res.s){
        $.Ajax("api/x_user", "POST", {}, function (res) {
          if (res.s) {
            wx.setStorageSync('UserInfor', res.d);
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          }
        }) 
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          image: '../../images/err.png',
          duration: 2000
        })
      }
    })
  },

  // 点击角色属性
  ClickAttribute: function(){
    // 申请成为领路人
    if (this.data.UserInfor.leadway == '' || this.data.UserInfor.leadway == null){
      wx.navigateTo({
        url: '../Guide/Guide',
      })

    // 正在审核中
    } else if (this.data.Status == 0){
      wx.navigateTo({
        url: '../UnderReview/UnderReview',
      })

    // 审核通过
    } else if (this.data.Status == 1) {
      wx.navigateTo({
        url: '../IReleased/IReleased',
      })
                                                                             
    // 审核失败
    }else{
      wx.navigateTo({
        url: '../Guide/Guide',
      })
    }
  },

  // 联系我们
  ContactUs: function(){
    wx.navigateTo({
      url: '../ContactUs/ContactUs',
    })
  },

  // 关于我们
  AboutUs: function(){
    wx.navigateTo({
      url: '../AboutUs/AboutUs',
    })
  },

  // 意见反馈
  FeedBack: function(){
    wx.navigateTo({
      url: '../FeedBack/FeedBack',
    })
  },

  // 我的余额
  Balance: function(){
    wx.navigateTo({
      url: '../Balance/Balance',
    })
  },

  // 我的订单
  MyOrder: function(){
    wx.navigateTo({
      url: '../MyOrder/MyOrder',
    })
  },

  // 我的收藏
  MyCollection: function(){
    wx.navigateTo({
      url: '../MyCollection/MyCollection',
    })
  },

  // 更新日志
  Developer: function(){
    wx.navigateTo({
      url: '../Developer/Developer',
    })
  }
})